package com.example.menuvirtualclient.util;

import java.io.*;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;

import com.example.menuvirtualclient.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.widget.Toast;

public class Common {

	public static String URL_WS = "http://192.168.0.5:1313/menuvirtualws/services/OperatorClass/";
	
	@SuppressWarnings("finally")
	public static byte[] ConvertImageToByteArray(String selectedImagePath) throws IOException{
		byte[] byteImage = null;
		try {
			
            FileInputStream instream = new FileInputStream(selectedImagePath);
            BufferedInputStream buffer = new BufferedInputStream(instream);
            byteImage = new byte[buffer.available()];
            buffer.read(byteImage);            
        } 
		catch (IOException ex) {
        	Log.e("Common", ex.getMessage());
        	throw ex;
        }
		finally{
			return byteImage;
		}
	}
	/*
	public static byte[] getfdf(R.drawable image){
		BitmapDrawable bmd = (BitmapDrawable) getResources().getDrawable(image);
        Bitmap bm = bmd.getBitmap();
        ByteBuffer byteBuff = ByteBuffer.allocate(bm.getWidth() * bm.getHeight() * 4);
        byteBuff.rewind();
        bm.copyPixelsToBuffer(byteBuff);
        byte[] tmp = new byte[bm.getWidth() * bm.getHeight() * 4];
        byteBuff.rewind();
        byteBuff.get(tmp);
        
        return tmp;
	}
	*/
	
	public static byte[] Encode(Bitmap icon) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        icon.compress(Bitmap.CompressFormat.PNG, 50, baos);
        byte[] data = baos.toByteArray();
        //String test = Base64.encodeBytes(data);
        //return test;
        return data;
    }
	
	public static double Round(double value) {
		  DecimalFormat twoDForm = new DecimalFormat("####.##");
		  return Double.valueOf(twoDForm.format(value));
	}
	
	public static float Round(float d, int decimalPlace) {
        BigDecimal bd = new BigDecimal(Float.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.floatValue();
    }
	
	public static double Round(double value, int decimalPlace) {
		double p = (double)Math.pow(10, decimalPlace);
		value = value * p;
		double tmp = Math.round(value);
		return (double)tmp/p;
	}
	
	public static void ShowToast(Context context, String message) {
		Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
		toast.show();
	}
}
